Thread: [Help] Reading in data from .csv file

  1. #16
    Registered User
    Join Date
    May 2017
    Posts
    11
    Code:
    530622021,20,02,4d,6,52000a58,00,111278fffffffffffff8
    530625446,20,02,86,6,52000a55,00,ffffff8e1937ffffffc8
    530256885,20,01,e5,3,52000a4a,00,3cffffffffffffffffff
    530256887,20,01,7a,3,52000a4b,00,1713161620ffffffffff
    530256886,20,01,dc,3,52000a4c,00,881b8ad8758c8dffffff
    530256816,20,01,a2,3,52000a4d,00,ffffffffffffffffffff
    530256884,20,01,52,3,52000a4e,00,18164af55e72a6ffffff
    530256885,20,01,e5,3,52000a4a,00,3cffffffffffffffffff
    530256887,20,01,7a,3,52000a4b,00,1713161620ffffffffff
    530256886,20,01,dc,3,52000a4c,00,881b8ad8758c8dffffff
    using this new code I made
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    int main() {
        // Declare Variables
        char direct[2048]; // Used to store the char string array of the current programs directory (so the program will work even if you move it around)
        getcwd(direct); // Gets the "current working directory" (cwd) of the program
        strcat(direct, "\\code.txt"); // Adds the name of the textfile on the end of the directory path
        char lineOfText[1000][200] = {0}; // Holds each line and character of the textfile
        FILE *myFileHand; // Used to handle the file
        int i = 0, k = 0, j = 0, ii = 0, iii = 0; // Counters
        int lines; // Used to count the number of lines in the textfile
        
        char time[1000][8]; //The time in seconds when it was sent
          char type[1000][2]; //Type code to idtentify the device (0x20)
          char version[1000][2] = {0}; //software version
          char counter[1000][2]; //A rolling 8-bit, ever increasing number. Used to show how many messages are being missed by the receiver
          char via[1000][1]; //Which receiver picked up this device’s transmission
          char address[1000][8]; //The address of the transmitter
          char status[1000][2]; //The status code of the device
          char sensorData[1000][128]; //10-bytes of sensor data in hex (each sensor being 1 byte and 10 sensors) 
    
        /*+==================================================+
        File Handling
        +==================================================+*/
    
        myFileHand = fopen("1000.csv", "r");
    
        if(myFileHand != NULL) { // reading the text file into a 2D array as well as counting the number of lines it has
    
            char line[150];
            while (fgets(line, 150, myFileHand) != NULL) {
                strcpy(lineOfText[j], line);
                printf("LINE: %s\n", lineOfText[j]);
                printf("LINENO: %d\n", j);
                j++;
                lines++;
            }
            fclose(myFileHand);
        }
    }
    I am able to read in the file.
    I just get a string of each line.
    The problem is separating them into each array.
    Last edited by darkrage453; 05-23-2017 at 08:54 AM.

  2. #17
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Code:
          char time[1000][8]; //The time in seconds when it was sent
          char type[1000][2]; //Type code to idtentify the device (0x20)
          char version[1000][2] = {0}; //software version
          char counter[1000][2]; //A rolling 8-bit, ever increasing number. Used to show how many messages are being missed by the receiver
          char via[1000][1]; //Which receiver picked up this device’s transmission
          char address[1000][8]; //The address of the transmitter
          char status[1000][2]; //The status code of the device
          char sensorData[1000][128]; //10-bytes of sensor data in hex (each sensor being 1 byte and 10 sensors)
    Why is everything a string instead of numbers?

    It looks like several of your strings don't have enough room for the "data". For example if the "time" of the first record is "530622021" your string must be at least a size 10 (char time[1000][10]) and if the "type" is "20" you need at least a size of 3, etc.

    But don't you want to have numbers for most of those fields instead of strings?


    Jim

  3. #18
    Registered User
    Join Date
    May 2017
    Posts
    11
    There are all strings because you are just looking at a small sample. I have to account for error messages being read in. I can't make some validation to dismiss it being read in because depending on the output will depend on the error message I will output.

  4. #19
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Well without knowing exactly what the input looks like it is almost impossible for me to help you.

    However if you do use strings the arrays must be large enough to hold the largest possible potential string, and don't forget to leave room for the end of string character.

    You should also consider using a struct to hold the records instead of the error prone parallel arrays.


    Jim

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Reading data from a file
    By SherryAli in forum C Programming
    Replies: 1
    Last Post: 04-04-2013, 03:44 PM
  2. Replies: 13
    Last Post: 05-31-2009, 11:30 AM
  3. It is not reading the data from the file...
    By musique in forum C++ Programming
    Replies: 5
    Last Post: 05-01-2009, 03:19 PM
  4. Replies: 2
    Last Post: 06-16-2005, 10:03 AM
  5. Reading in data from a file
    By neandrake in forum C++ Programming
    Replies: 8
    Last Post: 02-27-2005, 09:04 PM

Tags for this Thread